layui.define(['table', 'jquery', 'element','facade','laytpTab'], function(exports) {
	"use strict";

	var MOD_NAME = 'menu',
		$ = layui.jquery,
		element = layui.element;
    var laytpTab = layui.laytpTab;

	var laytpMenu = function(opt) {
		this.option = opt;
	};

	// 供外部调用的，渲染菜单方法
	laytpMenu.prototype.render = function(opt) {
		var option = {
			elem: opt.elem,
			async: opt.async,
			parseData: opt.parseData,
			url: opt.url,
			method: opt.method ? opt.method : "GET",
			defaultOpen: opt.defaultOpen,
			defaultSelect: opt.defaultSelect,
			control: opt.control,
			defaultMenu: opt.defaultMenu,
			accordion: opt.accordion,
			height: opt.height,
			theme: opt.theme,
			data: opt.data ? opt.data : [],
			change: opt.change ? opt.change : function() {},
			done: opt.done ? opt.done : function() {}
		};
		if (option.async) {
			$.ajax({
				url: server_api_name + option.url,
                type: option.method,
				data: {token: facade.getCookie('token'), pid: 0},
				dataType: 'json',
				success:function (res) {
					if(res.code === 2000) {
						localStorage.setItem('yi_menu', JSON.stringify(res.data));
                        if(res.data.length > 0) {
                            createMenuAndControl(res.data);
                        }
                        element.init();
					}
					if($("#sideMenu").attr('menukey') > 0){
                        $("#sideMenu").attr('menukey', 0);
                        $("#sideMenu ul li:first-child").click();
                    }
                }
			})
		}
		// 处理高度
		$("#"+opt.elem).height(option.height);
		return new laytpMenu(opt);
	};


	// 二级菜单任意顶部菜单绑定点击事件
	laytpMenu.prototype.click = function(clickEvent) {
        // 一级菜单点击事件
        $(document).off("click", "#sideMenu lixx").on("click", "#sideMenu lixx", function() {
            var menu_id = $(this).attr("menu-id");
            var menu_color = $(this).attr("menu-color");
            var menu_icon = $(this).attr("menu-icon");
            var menu_title = $(this).attr("menu-title");

            // 查询遍历二级菜单
            $.ajax({
                url: server_api_name + ask_getusermenu,
                type: 'post',
                data: {token: facade.getCookie('token'), pid: menu_id},
                dataType: 'json',
                success:function (res) {
                    if(res.code === 2000) {
                        localStorage.setItem('er_menu', JSON.stringify(res.data));

                        // 遍历二级菜单
                        if(res.data.length > 0) {
                            var control = '<ul class="layui-nav" lay-filter="control-nav">';
                            $.each(res.data, function(i, item) {
                                control += '<li class="layui-nav-item">' +
                                    '<a class="site-demo-active" href="javascript:;" menu-url="'+item.href+'" ' +
                                    'menu-id="' + item.id +'" menu-title="'+item.name+'">' + item.name + '</a>';

                                if(item.children.length > 0){
                                    control += '<dl class="layui-nav-child">';
                                    item.children.forEach(function (v,k) {
                                        control += '<dd><a href="javascript:;" menu-url="'+v.modedir+'" menu-id="'+v.id+'" menu-title="'+v.name+'" menu-pid="'+v.pid+'">' +
                                            '<span style="width: 15px;display: inline-flex;"><i class="layui-icon layui-icon-right" style="display: none"></i></span><span>'+v.name+'</span></a></dd>';
                                    });
                                    control += '</dl>';
                                }

                                control += '</li>';
                            });
                            control += '</ul>';
                            $("#control").html(control);
                            element.render('control-nav');
                        }


                        if($("#control").attr('menukey') > 0){
                            $("#control").attr('menukey', 0);
                            // $("#control ul li:first-child").find('.site-demo-active').click();
                            $("#control ul li:first-child").find('.site-demo-active').css({'border-bottom':'2px solid #1ca5ff','font-weight': 700});
                        }
                    }
                }
            });

            // 顶部左侧一级菜单信息
            var top_yi_menu_info = '<a href="javascript:void(0);"><i class="'+menu_icon+'" style="color:'+menu_color+';"></i>' +
                menu_title+'</a>';
            $('#header-left-title').html(top_yi_menu_info);

            // 设置顶部左侧一级菜单和活动的一级菜单id
            localStorage.setItem('top_yi_menu_info',top_yi_menu_info);
            localStorage.setItem('active_yi_menu',menu_id);

            // 一级菜单点击样式处理
            $(document).find("#sideMenu li").each(function () {
                let menucolor = $(this).attr("menu-color");
                $(this).css({'background': '#28333e'});
                $(this).find('a').css({'background': '#28333e','color': '#E5E8F1'});
                $(this).find('i').css({'color': menucolor});
            });
            $(this).css({'background': menu_color});
            $(this).find('a').css({'background': menu_color, 'color': '#fff'});
            $(this).find('i').css({'color': '#fff'});
        });

        // 二级菜单点击事件
        $(document).on("click", "#control li dd axx", function() {
            var menuId = $(this).attr('menu-id');
            var menuTitle = $(this).attr('menu-title');
            var menuPath = $(this).attr('menu-url');
            clickEvent($(this), {
                menuId: menuId,
                menuTitle: menuTitle,
                menuPath: menuPath,
                menuUrl: 'page/'+menuPath,
            });
        });



        // 二级菜单点击事件xxx
        $(document).off("click", "#control .site-demo-activexx").on("click", "#control .site-demo-activexx", function() {
            if($(this).parent().find('dl').length == 0){ // 判断没有三级菜单可以点击
                var dom = $(this);
                var data = {
                    menuId: dom.attr("menu-id"),
                    menuTitle: dom.attr("menu-title"),
                    menuPath: dom.attr("menu-title"),
                    menuIcon: dom.attr("menu-icon"),
                    menuColor: dom.attr("menu-color"),
                    menuUrl: dom.attr("menu-url"),
                    openType: dom.attr("open-type")
                };

                // 处理二级菜单点击样式问题
                $("#control li").each(function () {
                    $(this).find('a').css({'border-bottom':'none','font-weight': 500})
                });
                $(this).css({'border-bottom':'2px solid #1ca5ff','font-weight': 700});

                var er_menu = JSON.parse(localStorage.getItem('er_menu'));
                $.each(er_menu, function(i, note) {
                    // 判断是否存在二级菜单的选项，且按照分类保存，如果有则更新，没有在保存，保存当前打开的二级菜单的id
                    if(data.menuId == note.id){
                        // 保存二级id菜单
                        localStorage.setItem('active_er_menu',note.id);

                        // 切换菜单获取判断,二级菜单是否存在过已经打开过的三级菜单
                        var old_active_er_menu = localStorage.getItem('active_er_menu'+note.id);
                        if(old_active_er_menu){
                            // 存储三级菜单
                            if(note.children && note.children.length > 0){
                                $.each(note.children, function (i, note2) {
                                    if(old_active_er_menu == note2.id){
                                        // 存储三级菜单
                                        var active_san_menu = {
                                            id: note2['id'],
                                            title: note2['name'],
                                            href: 'page/'+note2['modedir'],
                                            pid: note2['pid'],
                                        };
                                        localStorage.setItem('active_san_menu',JSON.stringify(active_san_menu));
                                    }
                                })
                            }else{
                                if(old_active_er_menu == note.id){
                                    // 存储三级菜单
                                    var active_san_menu = {
                                        id: note['id'],
                                        title: note['name'],
                                        href: 'page/'+note['modedir'],
                                        pid: note['pid'],
                                    };
                                    localStorage.setItem('active_san_menu',JSON.stringify(active_san_menu));
                                }
                            }

                            element.tabChange('content', old_active_er_menu)
                        }else{
                            // 存在三级菜单直接展示三级菜单的第一个菜单, 否则展示二级菜单
                            if(note.children && note.children.length > 0){
                                // 存储二级菜单下打开的页面 key二级名称，val三级id
                                localStorage.setItem('active_er_menu'+note.id,note.children[0]['id']);
                                // 存储三级菜单
                                var active_san_menu = {
                                    id: note.children[0]['id'],
                                    title: note.children[0]['name'],
                                    href: 'page/'+note.children[0]['modedir'],
                                    pid: note.children[0]['pid'],
                                };
                                localStorage.setItem('active_san_menu',JSON.stringify(active_san_menu));
                                clickEvent(dom, {
                                    menuId: note.children[0]['id'],
                                    menuTitle: note.children[0]['name'],
                                    menuPath: note.children[0]['name'],
                                    menuIcon: note.children[0]['icon'],
                                    menuUrl: 'page/'+note.children[0]['modedir'],
                                    openType: note.children[0]['type'],
                                });
                            }else{
                                // 存储二级菜单下打开的页面 key二级名称，val三级id
                                localStorage.setItem('active_er_menu'+note.id,note.id);
                                // 存储三级菜单
                                var active_san_menu = {
                                    id: note['id'],
                                    title: note['name'],
                                    href: 'page/'+note['modedir'],
                                    pid: note['pid'],
                                };
                                localStorage.setItem('active_san_menu',JSON.stringify(active_san_menu));

                                clickEvent(dom, {
                                    menuId: note['id'],
                                    menuTitle: note['name'],
                                    menuPath: note['name'],
                                    menuIcon: note['icon'],
                                    menuUrl: 'page/'+note['modedir'],
                                    openType: note['type'],
                                });
                            }

                            // 处理历史菜单打开记录 直接打开二级菜单或打开三级菜单
                            var open_menu = sessionStorage.getItem('open_menu');
                            var menuArr = JSON.parse(open_menu);
                            menuArr.push(active_san_menu);
                            sessionStorage.setItem('open_menu', JSON.stringify(menuArr));

                            var str = '';
                            str += '<span class="box-span box_span_bac" key="'+active_san_menu.id+'">';
                            str += '<span class="box-title" onclick="box_jump('+active_san_menu.id+',\''+active_san_menu.href+'\',\''+active_san_menu.title+'\','+active_san_menu.pid+')">'+active_san_menu.title+'</span>';
                            str += '<span class="box-close" onclick="box_close(this, '+active_san_menu.id+',\''+active_san_menu.href+'\','+active_san_menu.pid+')"></span>';
                            str += '</span>';
                            $('#footer_box .box').append(str);
                            $('#footer_box #history_num').text(menuArr.length);
                        }
                    }
                });
            }
        })
	};


    // 二级菜单任意顶部菜单绑定点击事件
    laytpMenu.prototype.click = function(clickEvent) {
        // 一级菜单点击事件
        $(document).off("click", "#control li").on("click", "#control li", function() {
            var menu_id = $(this).attr("menu-id");
            var menu_color = $(this).attr("menu-color");
            var menu_icon = $(this).attr("menu-icon");
            var menu_title = $(this).attr("menu-title");

            // 查询遍历二级菜单
            $.ajax({
                url: server_api_name + ask_getusermenu,
                type: 'post',
                data: {token: facade.getCookie('token'), pid: menu_id},
                dataType: 'json',
                success:function (res) {
                    if(res.code === 2000) {
                        localStorage.setItem('er_menu', JSON.stringify(res.data));

                        // 遍历二级菜单
                        if(res.data.length > 0) {
                            var control = '<ul class="layui-nav layui-nav-tree" style="width: 120px;" lay-filter="control-nav">';
                            $.each(res.data, function(i, item) {
                                control += '<li class="layui-nav-item" style="position: relative;">' +
                                    '<a class="site-demo-active" href="javascript:;" menu-url="'+item.href+'" ' +
                                    'menu-id="' + item.id +'" menu-title="'+item.name+'">' + item.name + '</a>';


                                if(item.children.length > 0){
                                    control += '<div style="position: fixed;top: 0;left: 140px;background: red;display: none;">';

                                    control += '<ul>';
                                    item.children.forEach(function (v,k) {
                                        control += '<li><a href="javascript:;" menu-url="'+v.modedir+'" menu-id="'+v.id+'" menu-title="'+v.name+'" menu-pid="'+v.pid+'">' +
                                            '<span style="width: 15px;display: inline-flex;"><i class="layui-icon layui-icon-right" style="display: none"></i></span><span>'+v.name+'</span></a></li>';
                                    });
                                    control += '</ul>';

                                    control += '</div>';

                                }

                                control += '</li>';
                            });
                            control += '</ul>';
                            $("#sideMenu").html(control);
                            element.render('control-nav');
                        }


                        // if($("#control").attr('menukey') > 0){
                        //     $("#control").attr('menukey', 0);
                        //     // $("#control ul li:first-child").find('.site-demo-active').click();
                        //     $("#control ul li:first-child").find('.site-demo-active').css({'border-bottom':'2px solid #1ca5ff','font-weight': 700});
                        // }
                    }
                }
            });

            // 顶部左侧一级菜单信息
            var top_yi_menu_info = '<a href="javascript:void(0);"><i class="'+menu_icon+'" style="color:'+menu_color+';"></i>' +
                menu_title+'</a>';
            $('#header-left-title').html(top_yi_menu_info);

            // 设置顶部左侧一级菜单和活动的一级菜单id
            localStorage.setItem('top_yi_menu_info',top_yi_menu_info);
            localStorage.setItem('active_yi_menu',menu_id);

            // 一级菜单点击样式处理
            $(document).find("#control li").each(function () {
                let menucolor = $(this).attr("menu-color");
                $(this).css({'background': '#28333e'});
                $(this).find('a').css({'background': '#28333e','color': '#E5E8F1'});
                $(this).find('i').css({'color': menucolor});
            });
            $(this).css({'background': menu_color});
            $(this).find('a').css({'background': menu_color, 'color': '#fff'});
            $(this).find('i').css({'color': '#fff'});
        });




        // 二级菜单点击事件
        $(document).on("mouseover", "#sideMenu>ul>li", function() {
            console.log($(this).offset().top)
            var top = $(this).offset().top
            $(this).find('div').show().css('cssText','position: fixed;left: 120px;background: #454f59;color:#fff;top:'+top+'px')
            // var menuId = $(this).attr('menu-id');
            // var menuTitle = $(this).attr('menu-title');
            // var menuPath = $(this).attr('menu-url');
            // clickEvent($(this), {
            //     menuId: menuId,
            //     menuTitle: menuTitle,
            //     menuPath: menuPath,
            //     menuUrl: 'page/'+menuPath,
            // });
        });

        $(document).on("mouseout", "#sideMenu>ul>li", function() {

            $(this).find('div').hide();

            $(this).find('div>ul>li').mouseover(function () {
                $(this).parent().parent().show()
            });

            $(this).find('div>ul>li').mouseout(function () {
                $(this).parent().parent().hide()
            });


            //
            // $(this).css('cssText','display:none;')

            // console.log($(this).offset().top)
            // var top = $(this).offset().top

            // var menuId = $(this).attr('menu-id');
            // var menuTitle = $(this).attr('menu-title');
            // var menuPath = $(this).attr('menu-url');
            // clickEvent($(this), {
            //     menuId: menuId,
            //     menuTitle: menuTitle,
            //     menuPath: menuPath,
            //     menuUrl: 'page/'+menuPath,
            // });
        });



        // 二级菜单点击事件xxx
        $(document).off("click", "#control .site-demo-active").on("click", "#control .site-demo-active", function() {
            if($(this).parent().find('dl').length == 0){ // 判断没有三级菜单可以点击
                var dom = $(this);
                var data = {
                    menuId: dom.attr("menu-id"),
                    menuTitle: dom.attr("menu-title"),
                    menuPath: dom.attr("menu-title"),
                    menuIcon: dom.attr("menu-icon"),
                    menuColor: dom.attr("menu-color"),
                    menuUrl: dom.attr("menu-url"),
                    openType: dom.attr("open-type")
                };

                // 处理二级菜单点击样式问题
                $("#control li").each(function () {
                    $(this).find('a').css({'border-bottom':'none','font-weight': 500})
                });
                $(this).css({'border-bottom':'2px solid #1ca5ff','font-weight': 700});

                var er_menu = JSON.parse(localStorage.getItem('er_menu'));
                $.each(er_menu, function(i, note) {
                    // 判断是否存在二级菜单的选项，且按照分类保存，如果有则更新，没有在保存，保存当前打开的二级菜单的id
                    if(data.menuId == note.id){
                        // 保存二级id菜单
                        localStorage.setItem('active_er_menu',note.id);

                        // 切换菜单获取判断,二级菜单是否存在过已经打开过的三级菜单
                        var old_active_er_menu = localStorage.getItem('active_er_menu'+note.id);
                        if(old_active_er_menu){
                            // 存储三级菜单
                            if(note.children && note.children.length > 0){
                                $.each(note.children, function (i, note2) {
                                    if(old_active_er_menu == note2.id){
                                        // 存储三级菜单
                                        var active_san_menu = {
                                            id: note2['id'],
                                            title: note2['name'],
                                            href: 'page/'+note2['modedir'],
                                            pid: note2['pid'],
                                        };
                                        localStorage.setItem('active_san_menu',JSON.stringify(active_san_menu));
                                    }
                                })
                            }else{
                                if(old_active_er_menu == note.id){
                                    // 存储三级菜单
                                    var active_san_menu = {
                                        id: note['id'],
                                        title: note['name'],
                                        href: 'page/'+note['modedir'],
                                        pid: note['pid'],
                                    };
                                    localStorage.setItem('active_san_menu',JSON.stringify(active_san_menu));
                                }
                            }

                            element.tabChange('content', old_active_er_menu)
                        }else{
                            // 存在三级菜单直接展示三级菜单的第一个菜单, 否则展示二级菜单
                            if(note.children && note.children.length > 0){
                                // 存储二级菜单下打开的页面 key二级名称，val三级id
                                localStorage.setItem('active_er_menu'+note.id,note.children[0]['id']);
                                // 存储三级菜单
                                var active_san_menu = {
                                    id: note.children[0]['id'],
                                    title: note.children[0]['name'],
                                    href: 'page/'+note.children[0]['modedir'],
                                    pid: note.children[0]['pid'],
                                };
                                localStorage.setItem('active_san_menu',JSON.stringify(active_san_menu));
                                clickEvent(dom, {
                                    menuId: note.children[0]['id'],
                                    menuTitle: note.children[0]['name'],
                                    menuPath: note.children[0]['name'],
                                    menuIcon: note.children[0]['icon'],
                                    menuUrl: 'page/'+note.children[0]['modedir'],
                                    openType: note.children[0]['type'],
                                });
                            }else{
                                // 存储二级菜单下打开的页面 key二级名称，val三级id
                                localStorage.setItem('active_er_menu'+note.id,note.id);
                                // 存储三级菜单
                                var active_san_menu = {
                                    id: note['id'],
                                    title: note['name'],
                                    href: 'page/'+note['modedir'],
                                    pid: note['pid'],
                                };
                                localStorage.setItem('active_san_menu',JSON.stringify(active_san_menu));

                                clickEvent(dom, {
                                    menuId: note['id'],
                                    menuTitle: note['name'],
                                    menuPath: note['name'],
                                    menuIcon: note['icon'],
                                    menuUrl: 'page/'+note['modedir'],
                                    openType: note['type'],
                                });
                            }

                            // 处理历史菜单打开记录 直接打开二级菜单或打开三级菜单
                            var open_menu = sessionStorage.getItem('open_menu');
                            var menuArr = JSON.parse(open_menu);
                            menuArr.push(active_san_menu);
                            sessionStorage.setItem('open_menu', JSON.stringify(menuArr));

                            var str = '';
                            str += '<span class="box-span box_span_bac" key="'+active_san_menu.id+'">';
                            str += '<span class="box-title" onclick="box_jump('+active_san_menu.id+',\''+active_san_menu.href+'\',\''+active_san_menu.title+'\','+active_san_menu.pid+')">'+active_san_menu.title+'</span>';
                            str += '<span class="box-close" onclick="box_close(this, '+active_san_menu.id+',\''+active_san_menu.href+'\','+active_san_menu.pid+')"></span>';
                            str += '</span>';
                            $('#footer_box .box').append(str);
                            $('#footer_box #history_num').text(menuArr.length);
                        }
                    }
                });
            }
        })
    };



	// 样式选择
	laytpMenu.prototype.skin = function(skin) {
		var menu = $(".laytp-nav-tree[lay-filter='" + this.option.elem + "']").parent();
		menu.removeClass("dark-theme");
		menu.removeClass("light-theme");
		menu.addClass(skin);
	};

	// 选择没有子级的菜单节点
	laytpMenu.prototype.selectItem = function(laytpId) {
		if (this.option.control != false) {
			$("#" + this.option.elem + " a[menu-id='" + laytpId + "']").parents(".layui-side-scroll ").find("ul").css({
				display: "none"
			});
			$("#" + this.option.elem + " a[menu-id='" + laytpId + "']").parents(".layui-side-scroll ").find(".layui-this").removeClass(
				"layui-this");
			$("#" + this.option.elem + " a[menu-id='" + laytpId + "']").parents("ul").css({
				display: "block"
			});
			var controlId = $("#" + this.option.elem + " a[menu-id='" + laytpId + "']").parents("ul").attr("menu-id");
			if (controlId != undefined) {
				$("#" + this.option.control).find(".layui-this").removeClass("layui-this");
				$("#" + this.option.control).find("[menu-id='" + controlId + "']").addClass("layui-this");
			}
		}
		if (this.option.accordion === true) {
			$("#" + this.option.elem + " a[menu-id='" + laytpId + "']").parents(".laytp-nav-tree").find(".layui-nav-itemed").removeClass(
				"layui-nav-itemed");
		}
		$("#" + this.option.elem + " a[menu-id='" + laytpId + "']").parents(".laytp-nav-tree").find(".layui-this").removeClass(
			"layui-this");
		if (!$("#" + this.option.elem).is(".laytp-nav-mini")) {
			$("#" + this.option.elem + " a[menu-id='" + laytpId + "']").parents(".layui-nav-item").addClass("layui-nav-itemed");
			$("#" + this.option.elem + " a[menu-id='" + laytpId + "']").parents("dd").addClass("layui-nav-itemed");
		}
		$("#" + this.option.elem + " a[menu-id='" + laytpId + "']").parent().addClass("layui-this");
	}

	// 手机模式下，右下角按钮点击展开收缩左侧菜单事件
	laytpMenu.prototype.collaspe = function(time) {
		var elem = this.option.elem;
		var config = this.option;
		if ($("#" + this.option.elem).is(".laytp-nav-mini")) {
            $("#layui-side").css("cssText","width:120px !important");
            $("#layui-side .title").removeClass('none');
            $("#layui-side .logo").hide();
            $('.layui-header').removeClass('mobile-header');
            $('.layui-body').removeClass('mobile-body');
            $("#sideMenu .layui-side-scroll").css("cssText","width:120px !important");
            $("#sideMenu .layui-side-scroll .layui-nav").css("cssText","width:120px !important");
            $("#sideMenu .layui-side-scroll .layui-nav a").css("cssText","text-overflow: ellipsis");
            $("#" + this.option.elem).removeClass("laytp-nav-mini");
		} else {
            $("#layui-side").css("cssText","width:49px !important");
            $("#layui-side .title").addClass('none');
            $("#layui-side .logo").show();
            $('.layui-header').addClass('mobile-header');
            $('.layui-body').addClass('mobile-body');
            $("#sideMenu .layui-side-scroll").css("cssText","width:50px !important");
            $("#sideMenu .layui-side-scroll .layui-nav").css("cssText","width:50px !important");
            $("#sideMenu .layui-side-scroll .layui-nav a").css("cssText","text-overflow: unset");
            $("#" + this.option.elem).addClass("laytp-nav-mini");
		}
	};


	// 新建多系统菜单, 包括渲染静态html和绑定顶部菜单点击事件
    function createMenuAndControl1(data) {
        // 声 明 头 部
        var menu = '<div class="layui-side-scroll">';
        // 获取打开的一级菜单 以及 顶部显示的一级菜单样式
        var active_yi = 0;
        if(localStorage.getItem('active_yi_menu')){
            active_yi = localStorage.getItem('active_yi_menu');
        }
        if(localStorage.getItem('top_yi_menu_info')){
            var top_yi_menu_info = localStorage.getItem('top_yi_menu_info');
            $('#header-left-title').html(top_yi_menu_info);
        }

        var menuItem = '<ul class="layui-nav layui-nav-tree" style="width: 150px;">';
        $.each(data, function(i, item) {
            // 创 建 菜 单 结 构
            if(active_yi == item.id){
                menuItem += '<li menu-title="' + item.name + '" menu-id="' + item.id + '" ' +
                    'menu-color="'+item.color+'" menu-icon="'+item.icon+'" class="layui-nav-item" style="background: '+item.color+'">' +
                    '<a href="javascript:void(0);" style="color: #fff;">' +
                    '<i class="'+item.icon+'" style="font-size: 38px; color: #fff;"></i>&nbsp;&nbsp;&nbsp;'+ item.name + '</a></li>';

                // 查询二级菜单
                $.ajax({
                    url: server_api_name + ask_getusermenu,
                    type: 'post',
                    data: {token: facade.getCookie('token'), pid: item.id},
                    dataType: 'json',
                    success:function (res) {
                        if(res.code === 2000) {
                            localStorage.setItem('er_menu', JSON.stringify(res.data));
                            var active_er_menu = localStorage.getItem('active_er_menu');
                            // 遍历二级菜单
                            if(res.data.length > 0) {
                                var control = '<ul class="layui-nav">';
                                $.each(res.data, function(i, item) {
                                	if(active_er_menu == item.id){
                                        control += '<li class="layui-nav-item"><a class="site-demo-active" href="javascript:;" menu-url="'+item.href+'" ' +
                                            'menu-id="' + item.id +'" menu-title="'+item.name+'" style="border-bottom: 2px solid rgb(28, 165, 255); font-weight: 700;">' + item.name + '</a></li>';
                                	}else{
                                        control += '<li class="layui-nav-item"><a class="site-demo-active" href="javascript:;" menu-url="'+item.href+'" ' +
                                            'menu-id="' + item.id +'" menu-title="'+item.name+'">' + item.name + '</a></li>';
									}
                                });
                                control += 	'</dl></li></ul>';
                                $("#control").html(control);
                            }
                            // 打开三级菜单
                            if(localStorage.getItem('active_san_menu')){
                                var active_san = JSON.parse(localStorage.getItem('active_san_menu'));
                                laytpTab.render({
                                    elem: 'content',
                                    roll: true,
                                    tool: true,
                                    width: '100%',
                                    height: '100%',
                                    index: 2,
                                    id: active_san.id,
                                    url: active_san.href,
                                    title: active_san.title,
                                    close: true,
                                    news: 1, // 新增
                                });
                                localStorage.setItem('active_er_menu',active_er_menu);
                                localStorage.setItem('active_er_menu'+active_er_menu,active_san.id);
                            }
                        }
                    }
                });

            }else{
                menuItem += '<li menu-title="' + item.name + '" menu-id="' + item.id + '" ' +
                    'menu-color="'+item.color+'" menu-icon="'+item.icon+'" class="layui-nav-item">' +
                    '<a href="javascript:void(0);" style="letter-spacing: 1px;">' +
                    '<img src="/static/admin/images/menu/'+item.icon+'.png" style="height: 25px;width: 25px">' +
                    // '<i class="'+item.icon+'" style="font-size: 18px; color:'+item.color+';"></i>' +
                    '&nbsp;&nbsp;&nbsp;' + item.name + '</a></li>';
            }
        });
        menu += menuItem;
        menu += '</ul>';
        $("#sideMenu").html(menu);
    }



    function createMenuAndControl(data) {
        // 声 明 头 部
        // var menu = '<div class="layui-side-scroll">';
        // 获取打开的一级菜单 以及 顶部显示的一级菜单样式
        var active_yi = 0;
        if(localStorage.getItem('active_yi_menu')){
            active_yi = localStorage.getItem('active_yi_menu');
        }
        if(localStorage.getItem('top_yi_menu_info')){
            var top_yi_menu_info = localStorage.getItem('top_yi_menu_info');
            $('#header-left-title').html(top_yi_menu_info);
        }

        var menu = '';
        var menuItem = '<ul class="layui-nav">';
        $.each(data, function(i, item) {
            // 创 建 菜 单 结 构
            if(active_yi == item.id){
                menuItem += '<li menu-title="' + item.name + '" menu-id="' + item.id + '" ' +
                    'menu-color="'+item.color+'" menu-icon="'+item.icon+'" class="layui-nav-item" style="background: '+item.color+'">' +
                    '<a href="javascript:void(0);" style="color: #fff;">' +
                    '<i class="'+item.icon+'" style="font-size: 38px; color: #fff;"></i>&nbsp;&nbsp;&nbsp;'+ item.name + '</a></li>';

                // 查询二级菜单
                $.ajax({
                    url: server_api_name + ask_getusermenu,
                    type: 'post',
                    data: {token: facade.getCookie('token'), pid: item.id},
                    dataType: 'json',
                    success:function (res) {
                        if(res.code === 2000) {
                            localStorage.setItem('er_menu', JSON.stringify(res.data));
                            var active_er_menu = localStorage.getItem('active_er_menu');
                            // 遍历二级菜单
                            if(res.data.length > 0) {
                                var control = '<ul class="layui-nav layui-nav-tree" style="width: 120px;">';
                                $.each(res.data, function(i, item) {
                                    if(active_er_menu == item.id){
                                        control += '<li class="layui-nav-item"><a class="site-demo-active" href="javascript:;" menu-url="'+item.href+'" ' +
                                            'menu-id="' + item.id +'" menu-title="'+item.name+'" style="border-bottom: 2px solid rgb(28, 165, 255); font-weight: 700;">' + item.name + '</a></li>';
                                    }else{
                                        control += '<li class="layui-nav-item"><a class="site-demo-active" href="javascript:;" menu-url="'+item.href+'" ' +
                                            'menu-id="' + item.id +'" menu-title="'+item.name+'">' + item.name + '</a></li>';
                                    }
                                });
                                control += 	'</dl></li></ul>';
                                console.log(control)
                                $("#sideMenu").html(control);
                            }
                            // 打开三级菜单
                            if(localStorage.getItem('active_san_menu')){
                                var active_san = JSON.parse(localStorage.getItem('active_san_menu'));
                                laytpTab.render({
                                    elem: 'content',
                                    roll: true,
                                    tool: true,
                                    width: '100%',
                                    height: '100%',
                                    index: 2,
                                    id: active_san.id,
                                    url: active_san.href,
                                    title: active_san.title,
                                    close: true,
                                    news: 1, // 新增
                                });
                                localStorage.setItem('active_er_menu',active_er_menu);
                                localStorage.setItem('active_er_menu'+active_er_menu,active_san.id);
                            }
                        }
                    }
                });

            }else{
                menuItem += '<li menu-title="' + item.name + '" menu-id="' + item.id + '" ' +
                    'menu-color="'+item.color+'" menu-icon="'+item.icon+'" class="layui-nav-item">' +
                    '<a href="javascript:void(0);" style="letter-spacing: 1px;">' +
                    '<img src="/static/admin/images/menu/'+item.icon+'.png" style="height: 25px;width: 25px">' +
                    // '<i class="'+item.icon+'" style="font-size: 18px; color:'+item.color+';"></i>' +
                    '&nbsp;&nbsp;&nbsp;' + item.name + '</a></li>';
            }
        });
        menu += menuItem;
        menu += '</ul>';

        $("#control").html(menu);
    }


    exports(MOD_NAME, new laytpMenu());
});
